package com.zprmb.udzt.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.Regular_Adapter4;
import com.zprmb.udzt.bean.SanBiao;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.VolleryUtil;

public class Regular_fragment2 extends BaseFragment implements OnRefreshListener2<ListView>{
	private PullToRefreshListView lv_product_fragment;
	private ListView actualListView;
	private String title,token;
	private List<SanBiao> list=new ArrayList<SanBiao>();
	private List<SanBiao> list2=new ArrayList<SanBiao>();
	private Regular_Adapter4 adapter;
	TimerTask timerTask;
	private Timer timer;
	private int page;
	private ImageView img_none;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		title=getArguments().getString("title");
		token=getArguments().getString("token");
		page=1;
	}
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.regular_frament2, null);
		lv_product_fragment = (PullToRefreshListView) view.findViewById(R.id.lv);
		img_none=(ImageView)view.findViewById(R.id.iv_v4_nonetwork);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		lv_product_fragment.setOnRefreshListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		LogUtils.e(getActivity(), "creatview");
		handler.sendEmptyMessage(1);
		return view;
	}
	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if(jsonObject.optInt("success")==1){
					
					Gson gson=new Gson();
					if(title.equals("募集中")){
//						Log.e("募集中", jsonObject.toString());
						if (!jsonObject.optString("resultList").equals("null")) {
							list=gson.fromJson(jsonObject.optString("resultList"), new TypeToken<ArrayList<SanBiao>>() {
								}.getType());
							
						}
						if(list!=null){
							
							list2.addAll(list);
							Log.e("募集中", list2.toString());
							adapter=new Regular_Adapter4( getActivity(),list2,title,true);
							lv_product_fragment.setAdapter(adapter);
							adapter.notifyDataSetChanged();
							actualListView.setSelection(list2.size()-10);
							if(list2.size()<1){
								lv_product_fragment.setVisibility(View.GONE);
								img_none.setVisibility(View.VISIBLE);
							}else{
								lv_product_fragment.setVisibility(View.VISIBLE);
								img_none.setVisibility(View.GONE);
							}
						}
						timerTask = new TimerTask() {

							@Override
							public void run() {
								handler.sendEmptyMessage(3);
							}
						};
                    
                    	 if(timer!=null){
                    		 timer.cancel();
                    	 }
                    	 if(timer==null){
                    		 
                    		 timer = new Timer();
                    		 timer.schedule(timerTask, 0, 1000);
                    	 }
                     
					}else if(title.equals("已满标")){
						if (!jsonObject.optString("resultList").equals("null")) {
							list=gson.fromJson(jsonObject.optString("resultList"), new TypeToken<ArrayList<SanBiao>>() {
								}.getType());
							
						}
						list2.addAll(list);
						Log.e("已满标", list2.toString());
						adapter=new Regular_Adapter4( getActivity(),list2,title,true);
						lv_product_fragment.setAdapter(adapter);
						adapter.notifyDataSetChanged();
						actualListView.setSelection(list2.size()-10);
						if(list2.size()<1){
							lv_product_fragment.setVisibility(View.GONE);
							img_none.setVisibility(View.VISIBLE);
						}else{
							lv_product_fragment.setVisibility(View.VISIBLE);
							img_none.setVisibility(View.GONE);
						}
					}else if(title.equals("回款中")){
						
						if (!jsonObject.optString("resultList").equals("null")) {
							list=gson.fromJson(jsonObject.optString("resultList"), new TypeToken<ArrayList<SanBiao>>() {
								}.getType());
							
						}
						list2.addAll(list);
						Log.e("回款中", list2.toString());
						adapter=new Regular_Adapter4( getActivity(),list2,title,true);
						lv_product_fragment.setAdapter(adapter);
						adapter.notifyDataSetChanged();
						actualListView.setSelection(list2.size()-10);
						if(list2.size()<1){
							lv_product_fragment.setVisibility(View.GONE);
							img_none.setVisibility(View.VISIBLE);
						}else{
							lv_product_fragment.setVisibility(View.VISIBLE);
							img_none.setVisibility(View.GONE);
						}
					}else if(title.equals("已回款")){
						if (!jsonObject.optString("resultList").equals("null")) {
							list=gson.fromJson(jsonObject.optString("resultList"), new TypeToken<ArrayList<SanBiao>>() {
								}.getType());
							
						}
						if(list!=null){
							
							list2.addAll(list);
							Log.e("已回款", list2.toString());
							adapter=new Regular_Adapter4( getActivity(),list2,title,true);
							lv_product_fragment.setAdapter(adapter);
							adapter.notifyDataSetChanged();
							actualListView.setSelection(list2.size()-10);
							if(list2.size()<1){
								lv_product_fragment.setVisibility(View.GONE);
								img_none.setVisibility(View.VISIBLE);
							}else{
								lv_product_fragment.setVisibility(View.VISIBLE);
								img_none.setVisibility(View.GONE);
							}
						}
					}
				}
				
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", token);
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		if(title.equals("募集中")){			
			volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/claim/rmbboxInvestList?pageNo="+page+"&pageSize=10&status=OPENED", "");
		}else if(title.equals("已满标")){
			volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/claim/rmbboxInvestList?pageNo="+page+"&pageSize=10&status=FINISHED", "");
		}else if(title.equals("回款中")){
			volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/claim/rmbboxInvestList?pageNo="+page+"&pageSize=10&status=SETTLED", "");
		}else if(title.equals("已回款")){
			volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/claim/rmbboxInvestList?pageNo="+page+"&pageSize=10&status=CLEARED", "");
		}
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1; 
				list2.clear();
				initData();
			}else if(msg.what==2){
				page+=1;
				initData();
				
				
			}else if(msg.what==3){
				adapter.notifyDataSetChanged();
			}
			
			lv_product_fragment.onRefreshComplete();
		};
	};
	public static Regular_fragment2 newInstance(int position, String string,String token) {
		Regular_fragment2 uf = new Regular_fragment2();
		Bundle bundle=new Bundle();
		bundle.putString("token", token);
		bundle.putString("title", string);
		uf.setArguments(bundle);
		
		return uf;
	}
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}
	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}

}
